package com.andaily.web.controller.mobile.account.statistics;

import com.andaily.domain.dto.account.*;
import com.andaily.infrastructure.support.LogHelper;
import com.andaily.service.AccountService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/account_statistics")
public class AccountStatisticsController implements InitializingBean {

    /**
     * Logger
     */
    private static LogHelper log = LogHelper.create(AccountStatisticsController.class);

    @Autowired
    private AccountService accountService;

    @RequestMapping("index")
    public String index(AccountStatisticsDTO statisticsDTO, ModelMap model) throws Exception {
        log.debug("Call Account statistics. statisticsDTO [" + statisticsDTO + "]");
        statisticsDTO = accountService.loadAccountStatistics(statisticsDTO);
        model.addAttribute("statisticsDTO", statisticsDTO);
        return "account/statistics/account_statistics";
    }

    @RequestMapping("daily")
    public String daily(AccountDailyStatisticsDTO dailyStatisticsDTO, Model model) throws Exception {
        dailyStatisticsDTO = accountService.loadDailyStatistics(dailyStatisticsDTO);
        model.addAttribute("dailyStatisticsDTO", dailyStatisticsDTO);
        return "account/statistics/account_daily_statistics";
    }

    @RequestMapping("month")
    public String month(AccountMonthStatisticsDTO monthStatisticsDTO, Model model) throws Exception {
        monthStatisticsDTO = accountService.loadMonthStatistics(monthStatisticsDTO);
        model.addAttribute("monthStatisticsDTO", monthStatisticsDTO);
        return "account/statistics/account_month_statistics";
    }

    @RequestMapping("month/more")
    public String monthMore(AccountMonthStatisticsDTO monthStatisticsDTO, Model model) throws Exception {
        monthStatisticsDTO = accountService.loadMonthStatistics(monthStatisticsDTO);
        model.addAttribute("monthStatisticsDTO", monthStatisticsDTO);
        return "account/statistics/account_month_statistics_more";
    }

    @RequestMapping("type")
    public String type(AccountTypeStatisticsDTO typeStatisticsDTO, Model model) throws Exception {
        typeStatisticsDTO = accountService.loadTypeStatistics(typeStatisticsDTO);
        model.addAttribute("typeStatisticsDTO", typeStatisticsDTO);
        return "account/statistics/account_type_statistics";
    }

    @RequestMapping("payments")
    public String payments(Model model) {
        AccountPaymentsStatisticsDto paymentsStatisticsDto = accountService.loadAccountPaymentsStatisticsDto();
        model.addAttribute("paymentsStatisticsDto", paymentsStatisticsDto);
        return "account/statistics/account_payments_statistics";
    }

    @RequestMapping("type/more")
    public String typeMore(AccountTypeStatisticsDTO typeStatisticsDTO, Model model) throws Exception {
        typeStatisticsDTO = accountService.loadTypeStatistics(typeStatisticsDTO);
        model.addAttribute("typeStatisticsDTO", typeStatisticsDTO);
        return "account/statistics/account_type_statistics_more";
    }

    @RequestMapping("daily/more")
    public String dailyMore(AccountDailyStatisticsDTO dailyStatisticsDTO, Model model) throws Exception {
        dailyStatisticsDTO = accountService.loadDailyStatistics(dailyStatisticsDTO);
        model.addAttribute("dailyStatisticsDTO", dailyStatisticsDTO);
        return "account/statistics/account_daily_statistics_more";
    }


    //显示每种统计的数据明细
    @RequestMapping("show_details")
    public String showDetails(AccountStatisticsDetailsDTO statisticsDetailsDTO, Model model) throws Exception {
        statisticsDetailsDTO = accountService.loadStatisticsDetails(statisticsDetailsDTO);
        model.addAttribute("statisticsDetails", statisticsDetailsDTO);
        return "account/statistics/account_statistics_details";
    }

    @RequestMapping("show_details/more")
    public String showDetailsMore(AccountStatisticsDetailsDTO statisticsDetailsDTO, Model model) throws Exception {
        statisticsDetailsDTO = accountService.loadStatisticsDetails(statisticsDetailsDTO);
        model.addAttribute("statisticsDetails", statisticsDetailsDTO);
        return "account/statistics/account_statistics_details_more";
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(accountService);
    }
}
